package com.zpb.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zpb.framework.domain.ApiResult;
import com.zpb.framework.domain.dto.CommentDto;
import com.zpb.framework.domain.entity.Comment;
import com.zpb.framework.domain.entity.User;
import com.zpb.framework.domain.enums.ResultCodeEnum;
import com.zpb.framework.domain.vo.CommentVo;
import com.zpb.framework.domain.vo.PageVo;
import com.zpb.framework.mapper.CommentMapper;
import com.zpb.framework.mapper.UserMapper;
import com.zpb.framework.service.CommentService;
import com.zpb.framework.utils.BeanCopyUtil;
import com.zpb.framework.utils.DictCodeUtil;
import com.zpb.framework.utils.NumConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/12
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    public List<CommentVo> buildCommentVoTree(Long rootId,String commentType) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getRootId,rootId)
                .orderByDesc(Comment::getCreateTime)
                .eq(Comment::getType, commentType);
        // 查询当前父节点的直接子节点
        List<CommentVo> children = BeanCopyUtil.copyBeanList(commentMapper.selectList(wrapper),CommentVo.class);
        // 递归查询每个子节点的子节点
        for (CommentVo child : children) {
            List<CommentVo> grandChildren = buildCommentVoTree(child.getId(),child.getType());
            child.setChildren(grandChildren);
            if(child.getToCommentUserId()!=-1){
                child.setToCommentUserName(userMapper.selectById(child.getToCommentUserId()).getNickName());
                child.setUsername(userMapper.selectById(child.getCreateBy()).getNickName());
            }
        }
        return children;
    }

    @Override
    public ApiResult<PageVo> getCommentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        Page<Comment> commentPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Comment> commentWrapper = new LambdaQueryWrapper<>();
        commentWrapper.eq(Comment::getArticleId, articleId)
                .eq(Comment::getRootId, NumConst.MINUS_ONE)
                .orderByDesc(Comment::getCreateTime)
                .eq(Comment::getType, commentType);
        page(commentPage, commentWrapper);
        List<Comment> records = commentPage.getRecords();
        List<CommentVo> commentVos = BeanCopyUtil.copyBeanList(records, CommentVo.class);
        commentVos.stream()
                .forEach(commentVo -> {
                    commentVo.setChildren(buildCommentVoTree(commentVo.getId(), commentType));
                    User user = userMapper.selectById(commentVo.getCreateBy());
                    if (user != null) {
                        commentVo.setUsername(user.getNickName());
                    }
                });
        PageVo pageVo = new PageVo(commentPage.getTotal(),commentVos);
        return ApiResult.success(pageVo);
    }

    @Override
    public ApiResult addComment(CommentDto commentDto) {
        Comment comment = BeanCopyUtil.copyBean(commentDto, Comment.class);
        int result = commentMapper.insert(comment);
        if(result>0){
            return ApiResult.success();
        }

        return ApiResult.failure(ResultCodeEnum.ADD_FAILURE);
    }

}
